// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/intrusion/api/IasDefenceApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/intrusion/model/QueryDeviceIasDefenceReq'
import OneClass from '@api/intrusion/model/QueryOneReq'
import EditClass from '@api/intrusion/model/SaveDeviceIasDefenceReq'
import DelClass from '@api/intrusion/model/DelDeviceIasDefenceReq'
import SubmitClass from '@api/intrusion/model/SyncDeviceIasDefenceReq'
import BaseClass from '@api/intrusion/model/DeviceIasDefenceDto'
import DeviceClass from '@api/intrusion/model/DeviceDto'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']
const BASE = [BaseClass, 'content']
const DEVICE = [DeviceClass, 'bindDevice', 1]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'queryDeviceIasDefence', DTO)
    return [resp.comQuery.totalCount, resp.content || [], resp.queryResult || {}]
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询详情') {
    const DTO = CORE.grpcDTO(OneClass, { id })
    const { content: list } = await CORE.grpc(TIP, CLIENT, 'queryOne', DTO)
    const { deviceIasDefenceDto = {}, alarmDtos = [], bindDevice = [] } = CORE.isEmpty(list) ? {} : list[0]
    return { ...deviceIasDefenceDto, alarmDtos, bindDevice }
  },
  transOne(obj) {
    obj.oldAreaId = obj.areaId
    return obj
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE, DEVICE)
    await CORE.grpc(TIP, CLIENT, 'saveDeviceIasDefence', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id, defenceId }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { id, defenceId })
    await CORE.grpc(TIP, CLIENT, 'delDeviceIasDefence', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 提交 —————————————————————————————— //

  async submit(one, TIP = '提交') {
    const DTO = CORE.grpcDTO(SubmitClass, one)
    await CORE.grpc(TIP, CLIENT, 'syncDeviceIasDefence', DTO)
    return CORE.grpcOK(TIP)
  },
}
